package com.tz.fruit.entity;

import com.tz.fruit.validation.*;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
* 商品实体类:
*/
@Entity
@Table(name="FRUIT_PRODUCT")
public class Product implements Serializable{
	private Long pid;//商品ID

	@Pattern(regexp="[a-zA-Z0-9]{4,12}",message = "{product.pNo}",groups = Group1.class)
	private String pNo;//
	private String imgUrl;//图片  -浏览、查询商品时显示的封面图片
	@NotBlank(message = "{product.pName}",groups = Group2.class)
	private String pName;//商品名称  -巨无霸-佳沛新西兰阳光金奇异
	@NotNull(message = "{product.price}",groups = Group3.class)
	@Min(value = 0,groups = Group3.class,message = "{product.price}")
	private Double price;//价格
	@NotNull(message = "{product.oldPrice}",groups = Group4.class)
	@Min(value = 0,groups = Group4.class,message = "{product.oldPrice}")
	private Double oldPrice;//原价
	@NotBlank(message = "{product.standard}",groups = Group5.class)
	private String standard;//规格  -如：4个  4斤 4盒... (单品、礼盒)
	@NotNull(message = "{product.stock}",groups = Group6.class)
	@Min(value = 0,groups = Group6.class,message = "{product.stock}")
	private Long stock;//库存
	private  RecState recState;//推荐类型-枚举  推荐产品,新品上市,热卖产品


	private int onShelves;//上下架- 0：下架  1：上架
	//多条件组合查询
	@NotBlank(message = "{product.origin}",groups = Group7.class)
	private String origin;//产地 新西兰

	private Category category;//品类 -奇异果  枚举 一对一单向关联


	
	public Product() {
	}

	public Product(String pNo, String imgUrl, String pName, Double price, Double oldPrice, String standard, Long stock,
			int onShelves, RecState recState, String origin, Category category) {
		super();
		this.pNo = pNo;
		this.imgUrl = imgUrl;
		this.pName = pName;
		this.price = price;
		this.oldPrice = oldPrice;
		this.standard = standard;
		this.stock = stock;
		this.onShelves = onShelves;
		this.recState=recState;
		this.origin = origin;
		this.category = category;
	}

	@Id
	@GeneratedValue
	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}
	
	@Column(nullable=false,unique=true)
	public String getpNo() {
		return pNo;
	}


	public void setpNo(String pNo) {
		this.pNo = pNo;
	}


	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	public String getpName() {
		return pName;
	}


	public void setpName(String pName) {
		this.pName = pName;
	}


	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getOldPrice() {
		return oldPrice;
	}

	public void setOldPrice(Double oldPrice) {
		this.oldPrice = oldPrice;
	}

	public String getStandard() {
		return standard;
	}

	public void setStandard(String standard) {
		this.standard = standard;
	}
	@ManyToOne(cascade = CascadeType.MERGE)
	@JoinColumn(name="categoryId")
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public String getOrigin() {
		return origin;
	}

	public void setOrigin(String origin) {
		this.origin = origin;
	}
	
	public Long getStock() {
		return stock;
	}


	public void setStock(Long stock) {
		this.stock = stock;
	}
	
	public int getOnShelves() {
		return onShelves;
	}

	public void setOnShelves(int onShelves) {
		this.onShelves = onShelves;
	}

	@Column
	@Enumerated(EnumType.STRING)
	public RecState getRecState() {
		return recState;
	}

	public void setRecState(RecState recState) {
		this.recState = recState;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((pid == null) ? 0 : pid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Product other = (Product) obj;
		if (pid == null) {
			if (other.pid != null)
				return false;
		} else if (!pid.equals(other.pid))
			return false;
		return true;
	}

	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder("Product{");
		sb.append("pNo='").append(pNo).append('\'');
		sb.append(", imgUrl='").append(imgUrl).append('\'');
		sb.append(", pName='").append(pName).append('\'');
		sb.append(", price=").append(price);
		sb.append(", oldPrice=").append(oldPrice);
		sb.append(", standard='").append(standard).append('\'');
		sb.append(", stock=").append(stock);
		sb.append(", onShelves=").append(onShelves);
		sb.append(", origin='").append(origin).append('\'');
		sb.append(", category=").append(category);
		sb.append(", recState=").append(recState);
		sb.append('}');
		return sb.toString();
	}
}
